package com.zxl.manager.controller;

import com.google.gson.Gson;
import com.zxl.manager.entity.ProdRes;
import com.zxl.manager.entity.Product;
import com.zxl.manager.entity.ProductDetail;
import com.zxl.manager.service.ProductService;
import com.zxl.manager.utils.ImageUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jimmy on 2017/7/9.
 */
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    Gson gson = new Gson();

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("prod/index");
        return mv;
    }

    @RequestMapping("/getAllProd")
    @ResponseBody
    public String getAllProd(Integer limit,Integer offset){
        Map<String, Object> resMap = new HashMap<>();
        List<Product> list = productService.getAllProd();
        resMap.put("total",list.size());
        resMap.put("rows",list);
        String res = gson.toJson(resMap);
        return res;
    }

    @RequestMapping("/insProd")
    @ResponseBody
    public String insProd(String jsonstr,HttpSession httpSession){
        System.out.println(jsonstr);
        ProdRes pr = gson.fromJson(jsonstr,ProdRes.class);
        System.out.println(pr.getProdName());
        Product product = new Product();

        Integer res = 0;

        if(StringUtils.isEmpty(pr.getProdId())) {
            product.setProductName(pr.getProdName());
            product.setProductDesc(pr.getProdDesc());
            product.setProductIcon("http://image.bearworker.com/" + pr.getPics().get(0).getPic());
            product.setProductDiscountPrice(Double.parseDouble(pr.getProdDisPrice()));
            product.setProductPrice(Double.parseDouble(pr.getProdPrice()));
            product.setProductMinPrice(Double.parseDouble(pr.getProdMinPrice()));
            product.setProductTypeId(Long.parseLong(pr.getProdType()));
            product.setProductMain(pr.getProdMain());
            res = productService.insProd(product);
        }else {
            product.setProductId(Long.parseLong(pr.getProdId()));
            product.setProductName(pr.getProdName());
            product.setProductDesc(pr.getProdDesc());
            product.setProductIcon("http://image.bearworker.com/" + pr.getPics().get(0).getPic());
            product.setProductDiscountPrice(Double.parseDouble(pr.getProdDisPrice()));
            product.setProductPrice(Double.parseDouble(pr.getProdPrice()));
            product.setProductMinPrice(Double.parseDouble(pr.getProdMinPrice()));
            product.setProductTypeId(Long.parseLong(pr.getProdType()));
            product.setProductMain(pr.getProdMain());
            res = productService.updProd(product);
        }

        List<ProductDetail> pdlist = new ArrayList<>();
        String leftPath = httpSession.getServletContext().getRealPath("/image");
        for(ProdRes.pic pic : pr.getPics()){
            ProductDetail pd = new ProductDetail();
            ImageUtil.upload(pic.getPic(),leftPath);
            pd.setType("dpic");
            pd.setValue("http://image.bearworker.com/"+pic.getPic());
            pd.setProductId(res.longValue());
            pdlist.add(pd);
        }

        for(ProdRes.pic pic : pr.getMainPic()){
            ProductDetail pd = new ProductDetail();
            ImageUtil.upload(pic.getPic(),leftPath);
            pd.setType("mpic");
            pd.setValue("http://image.bearworker.com/"+pic.getPic());
            pd.setProductId(res.longValue());
            pdlist.add(pd);
        }
        ProductDetail pd1 = new ProductDetail();
        pd1.setType("desc");
        pd1.setValue(pr.getProdDetail());
        pd1.setProductId(res.longValue());
        pdlist.add(pd1);
        ProductDetail pd2 = new ProductDetail();
        pd2.setType("spec");
        pd2.setValue(pr.getProdSpec());
        pd2.setProductId(res.longValue());
        pdlist.add(pd2);
        ProductDetail pd3 = new ProductDetail();
        pd3.setType("ston");
        pd3.setValue(pr.getProdSton());
        pd3.setProductId(res.longValue());
        pdlist.add(pd3);
        ProductDetail pd4 = new ProductDetail();
        pd3.setType("sendfee");
        pd3.setValue(pr.getProdSendFee());
        pd3.setProductId(res.longValue());
        pdlist.add(pd3);

        productService.insProdDet(pdlist);

        Map resMap = new HashMap();
        resMap.put("code","0");
        resMap.put("msg",res);
        return gson.toJson(resMap);
    }

    @RequestMapping("/delProds")
    @ResponseBody
    public String delProds(String ids){
        System.out.println(ids);
        Integer res = productService.delProd(ids);
        Map resMap = new HashMap();
        resMap.put("code","0");
        resMap.put("msg",res);
        return gson.toJson(resMap);
    }

    @RequestMapping("/getProdDet")
    @ResponseBody
    public String getProdDet(String prodId){
        List<ProductDetail> pdlist = productService.getProdDet(prodId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("total",pdlist.size());
        resMap.put("rows",pdlist);
        return gson.toJson(resMap);
    }
}
